package com.ftg.learn.amqp.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 布衣
 */
public class Ctopic {

    public static void main(String[] args) throws IOException, TimeoutException {
        //基础信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.2.40.82");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/demo");

        //拿到连接
        Connection connection = factory.newConnection();
        //创建channel
        Channel channel = connection.createChannel();

        String exName = "myTopic";
        String routingKey = "warning.abc.*";

        channel.exchangeDeclare(exName, BuiltinExchangeType.TOPIC);

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, exName, routingKey);

        channel.basicConsume(queueName, true,
                (consumerTag, message) -> System.out.println("接收到消息：" + new String(message.getBody())),
                consumerTag -> {});

    }
}